/*
 * Copyright (C) 2008 Wim Crols <twimpiex@gnx.be>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package be.gnx.event.consumer.application.action;

import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

import be.gnx.event.consumer.application.ui.EventConsumer;

public class Exit implements Listener, SelectionListener {

  private EventConsumer eventConsumer;

  public Exit(EventConsumer eventConsumer) {
    this.eventConsumer = eventConsumer;
  }

  public void handleEvent(Event event) {
    exit(null, event);
  }

  public void widgetDefaultSelected(SelectionEvent selectionEvent) {
    Exit.this.widgetSelected(selectionEvent);
  }

  public void widgetSelected(SelectionEvent selectionEvent) {
    exit(selectionEvent, null);
  }

  public void exit(SelectionEvent selectionEvent, Event event) {
    Display display = null;
    if (selectionEvent != null) {
      display = selectionEvent.display;
    } else if (event != null) {
      display = event.display;
    } else {
      return;
    }

    if (selectionEvent != null) {
      for (Shell shell : display.getShells()) {
        shell.close();
      }
    }
  }

}
